import java.io.*;
import java.util.*;

public class FileHandler {
	
	public String[] getDirectoryList() throws Exception
  {
    return getDirectoryList(".");
  }
  
  public String[] getDirectoryList(String s) throws Exception
  {
    return new File(s).list();
  }
  
  public ArrayList getTextFiles() throws Exception
  {
    return getTextFiles(".");
  }
  
  public ArrayList getTextFiles(String s) throws Exception
  {
    String[] children = new File(s).list();
    ArrayList result = new ArrayList();
    
    if (children == null) {
      return null;
    } else {
        for (int i=0; i<children.length; i++) 
        {
          if(children[i].indexOf(".txt") > 0 || children[i].indexOf(".TXT") > 0)
            result.add(children[i]);
        }
       
      return result;
    } 
     
  }
  
  public void processFile(String s) throws Exception
  {
    File input = new File(s);  
    Scanner scanner = new Scanner(input);
    
      try{
        //first use a Scanner to get each line
        while ( scanner.hasNextLine() ){
          processLine( scanner.nextLine() );
        }
      }catch(java.util.NoSuchElementException e){}
      
    
      //ensure the underlying stream is always closed
      scanner.close();
  }
  
  protected void processLine(String s) throws Exception
  {
    //use a second Scanner to parse the content of each line 
    HashMap m = new HashMap();
    int i=0;
    Scanner scanner = new Scanner(s);
    try{
      while(scanner.hasNext())
      {
        
        //System.out.println("i=" + i);
        //System.out.println(scanner.next());
        
        switch (i)
        {
          case 0: m.put("timePeriod", scanner.next().trim()); break;
          case 1: String v = scanner.next().trim();
                  String reportLevel = getReportLevel(v);
                  m.put("fileId", v); 
                  m.put("dealerCode", getDealerCode(v)); 
                  m.put("reportLevel", reportLevel); 
                  m.put("reportLevelValue", getReportLevelValue(reportLevel)); 
                  break;
          case 2: m.put("fileName", scanner.next().trim()); break;
          case 3: m.put("directoryStructure", scanner.next().trim()); break;
        }
        //System.out.println(scanner.next());
        
        
        i++;
        if (i>3)
          i=0;
          
      }
      
     System.out.println("File Name: " + m.get("dealerCode"));
    }catch(java.util.NoSuchElementException e){}
    //(no need for finally here, since String is source)
    scanner.close();
  }
  
   private String quote(String aText){
    String QUOTE = "'";
    return QUOTE + aText + QUOTE;
  }
  
  protected String getDealerCode(String s) throws Exception
  {
    // first char in string must equal D else return null
    // last 5 characters of string
    return "";
    
  }
  
  protected String getReportLevel(String s) throws Exception
  {
    return "First character in S";
  }
  
  protected String getReportLevelValue(String s) throws Exception
  {
    // first char in string must equal D else return null
    // last 5 characters of string
    return "";
    
  }


  
  public static void main (String args[]) throws Exception
 {
    // search the directory for text files
    // create an array of text files in directory
    // loop through array of text files
    // for each line in text file
      // create hashMap
   FileHandler fh  = new FileHandler();
   //fh.processFile("test.txt");
   fh.getDealerCode("DNE0251475");
   /*
   ArrayList ar = fh.getTextFiles();
   for (int i=0;i<ar.size();i++)
   {
     fh.processFile((String)ar.get(i));
   }
   */
   
 } 
  
  

}
